package com.llt.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomConfig> implements Ordered {

    public CustomGatewayFilterFactory() {
        super(CustomConfig.class);
    }

    @Override
    public GatewayFilter apply(CustomConfig config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // pro类型  执行请求(chain.filter)  post类型
                log.info("pro filter, config: {}", config); // pro类型的过滤器的代码逻辑

                return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                    log.info("post filter..."); // post类型的过滤器的代码逻辑
                }));
            }
        };
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }
}
